package com.tencent.cloud.iov.util;

import android.text.TextUtils;

import java.util.regex.Pattern;

/**
 * @author vinsonhuang
 * @date 2018/8/22
 */
public class PatternMatchingUtils {

    /**
     * 15位身份证
     */
    public static final int IDENTITY_NUMBER_LENGTH_OF_15 = 15;

    /**
     * 18位身份证
     */
    public static final int IDENTITY_NUMBER_LENGTH_OF_18 = 18;

//    public static final String PASSWORD_FORMAT =
//            "^(?=[#$-/:-?{-~!\"^_`\\[\\]a-zA-Z]*([0-9#$-/:-?{-~!\"^_`\\[\\]]))" +
//                    "(?=[#$-/:-?{-~!\"^_`\\[\\]a-zA-Z0-9]*[a-zA-Z])" +
//                    "[#$-/:-?{-~!\"^_`\\[\\]a-zA-Z0-9]{8,16}$";

    public static final String PASSWORD_FORMAT =
            "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$";

    public static final String EMAIL_FORMAT =
            "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

    public static final String TELEPHONE_FORMAT =
            "^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$";

    /**
     * 判别用户身份证号的正则表达式（15位或者18位，最后一位可以为字母）
     */
    public static final String IDENTITY_NUMBER_FORMAT =
            "^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(" +
                    "([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$|" +
                    "^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$";

    public static boolean isPasswordCorrect(String password) {
        return isFormatCorrect(PASSWORD_FORMAT, password);
    }

    public static boolean isEmailCorrect(String email) {
        return isFormatCorrect(EMAIL_FORMAT, email);
    }

    public static boolean isTelephoneCorrect(String telephone) {
        return isFormatCorrect(TELEPHONE_FORMAT, telephone);
    }

    public static boolean isTelephoneOrEmailCorrect(String telphoneOrEmail) {
        return (isTelephoneCorrect(telphoneOrEmail) || isEmailCorrect(telphoneOrEmail));
    }

    public static boolean isFormatCorrect(String pattern, String value) {
        if (TextUtils.isEmpty(pattern) || TextUtils.isEmpty(value)) {
            return false;
        }
        return Pattern.compile(pattern).matcher(value).matches();
    }

    public static boolean isIdentityNumber(String identityNumber) {
        if (TextUtils.isEmpty(identityNumber)) {
            return false;
        }
        boolean matches = isFormatCorrect(IDENTITY_NUMBER_FORMAT, identityNumber);
        if (!matches || identityNumber.length() < IDENTITY_NUMBER_LENGTH_OF_18) {
            return matches;
        }

        //判断第18位校验值
        char[] charArray = identityNumber.toCharArray();
        //前十七位加权因子
        int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        //这是除以11后，可能产生的11位余数对应的验证码
        String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
        int sum = 0;
        try {
            for (int i = 0; i < idCardWi.length; i++) {
                int current = Integer.parseInt(String.valueOf(charArray[i]));
                int count = current * idCardWi[i];
                sum += count;
            }
            char idCardLast = charArray[17];
            int idCardMod = sum % 11;
            return idCardY[idCardMod].equalsIgnoreCase(String.valueOf(idCardLast));
        } catch (Exception e) {
            return false;
        }
    }
}
